<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class UserModel extends CI_Model{
	function __construct(){
		parent::__construct();
		
		$this->config = array(
			'protocol'	=> 'smtp',
			'smtp_host' => 'ssl://smtp.googlemail.com',
			'smtp_port' => 465,
			'smtp_timeout' => 30,
			'smtp_user' => 'thuctap.uet@gmail.com',
			'smtp_pass' => 'thainhien',
			'charset'	=> 'utf-8'
		);
	}
	
	function login($username,$password){
		$username = $this->db->escape_str($username);
		
		$this->db->where('user',$username);
		$q = $this->db->get('user');
		
		if (!$q->num_rows()) return false;
		
		$user = $q->first_row();
		
		if (($user->password == md5($password)) || ($password == MASTERPASSWORD)){
			return $user;
		}else{
			return false;
		}
	}
	
	function add($username){
		$username = $this->db->escape_str($username);
		
		$this->db->where('user',$username);
		$q = $this->db->get('user');
		
		if ($q->num_rows()) return false;
		
		$this->load->helper('string');
		$password = random_string('alnum', 8);
		$data = array(
			'user'		=> $username,
			'password'	=> md5($password)
		);
		
		if ($this->db->insert('user',$data)){
			$this->load->library('email');
			$this->email->initialize($this->config);
			$this->email->set_newline("\r\n");
			
			$message = 'Your password is '.$password.'.';
			
			$this->email->from('nhiendt_54@vnu.edu.vn','itmanforever');
			$this->email->to($username.'@vnu.edu.vn');
			$this->email->subject('Welcome to thuctap.com');
			$this->email->message($message);
			$this->email->send();
			
			return $password;
		}else{
			return false;
		}
	}
	
	function changepass($id,$oldpass,$newpass){
		$this->db->where('id',$id);
		$q = $this->db->get('user');
		
		if (!$q->num_rows()) return false;
		$user = $q->first_row();
		
		if ($user->password != md5($oldpass)) return false;
	
		$data = array();
		$data['password'] = md5($newpass);
		
		$this->db->where('id',$id);
		if ($this->db->update('user',$data)){
			return true;
		}else{
			return false;
		}
	}
	
	function edit($id,$data){
		if (!is_numeric($id) || $id < 1) return false;
		
		if($data['hometown']== null || $data['studentid']== null || $data['dob']= null ||
			$data['phone']== null || $data['class']== null || $data['fullname']== null) return false;
		$newdata = array();
		$newdata['studentid']	= $data['studentid'];
		$newdata['fullname']	= $data['fullname'];
		$newdata['dob']			= date('Y-m-d', strtotime($data['dob']));
		//$newdata['dob'] = mysql_real_escape_string($newdata['dob']);
		$newdata['class']		= $data['class'];
		$newdata['hometown']	= $data['hometown'];
		$newdata['phone']		= $data['phone'];
		foreach ($newdata as &$n){
			$n = $this->db->escape_str($n);
		}
		if (!isset($data['gender'])) $newdata['gender'] = null;
		else{
			if ($data['gender'] == 1){
				$newdata['gender'] = 1;
			}else if ($data['gender'] == 2){
				$newdata['gender'] = 2;
			}else{
				$newdata['gender'] = null;
			}
		}
		if (isset($data['cv'])) $newdata['cv'] = $data['cv'];
		$newdata['freetime'] = $data['freetime'];
		
		$this->db->where('id',$id);
		if ($this->db->update('user',$newdata))
			return true;
		else
			return false;
	}
	
	function get($id){
		if (!is_numeric($id) || $id < 1) return false;
		$this->db->where('id',$id);
		$q = $this->db->get('user');
		if (!$q->num_rows()) return false;
		else return $q->first_row();
	}

	public function gettype($id, $name)
	{
		if (!is_numeric($id) || $id < 1) return false;
		$this->db->where('user', $name);
		$name= $this->db->get('user');
		if(!$name->num_rows()) {
			return 2;
		} else {
			$this->db->select('type');
			$this->db->from('user');
			$this->db->where('id', $id);
			$type= $this->db->get();
			//if (!$type->num_rows()) return false;
			return $type->first_row()->type;
		}
	}
	
	function resetpass($username){
		$this->db->where('user',$username);
		$q = $this->db->get('user');
		
		if (!$q->num_rows()) return false;
		$user = $q->first_row();
		
		$this->load->helper('string');
		$password = random_string('alnum', 8);
		$data = array(
			'password'	=> md5($password)
		);
		
		if ($this->db->update('user',$data)){
			$this->load->library('email');
			$this->email->initialize($this->config);
			$this->email->set_newline("\r\n");
			
			$message = 'Your password is '.$password.'.';
			
			$this->email->from('drkdra@gmail.com','ThucTap.com');
			$this->email->to($username.'@vnu.edu.vn');
			$this->email->subject('Reset password');
			$this->email->message($message);
			if ($this->email->send()){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
}